/*
 * Created on May 22, 2004
 * 
 * Vectrics Inc. GNU Public License.
 */
package org.vectrics.struts;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.http.HttpUtils;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;

import com.hexacta.hrs.securitygroups.Permission;
import com.hexacta.hrs.securitygroups.SecurityGroup;

/**
 * @author mmoore
 */
public abstract class VectricsTagSupport extends TagSupport {

    private static final long serialVersionUID = -8301378555787081858L;

    // private String contextPath = null;
    // private HttpServletRequest request = null;
    private Request request = null;

    protected final Logger log = Logger.getLogger(this.getClass());

    private String permission = null;

    public VectricsTagSupport() {
        super();
    }

    abstract protected String makeStartTag();

    protected String getContextPath() {
        return (request.getContextPath());
    }

    /*
     * protected HttpServletRequest getRequest() { return(request); }
     */

    protected Request getRequest() {
        return (this.request);
    }

    protected Locale getLocale() {
        return (request.getLocale());
    }

    protected String getPhrase(String code) {
        return (CoreServiceLocator.getPhraseProvider().getMessage(code, this.getLocale()));
    }

    protected String getPhrase(String code, Object[] args) {
        return (CoreServiceLocator.getPhraseProvider().getMessage(code, this.getLocale(), args));
    }

    protected Person getAuthPerson() {
        Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(this.getRequest());
        return (person);
    }

    @Override
    public final int doStartTag() throws JspException {
        log.debug("doStartTag() method called");

        int result = EVAL_PAGE;
        String html = "";
        try {
            request = HttpRequestConverter.convertRequest((HttpServletRequest) this.pageContext.getRequest());
            HttpUtils.logRequestInfo(this.getRequest(), log, Level.DEBUG);
            log.debug("doStartTag() calling makeStartTag()");
            boolean access = hasPermission();
            if (access) {
                html = makeStartTag();
            }
        } catch (Throwable t) {
            log.error("An error occurred building the form.  Form = " + this.getClass().getName(), t);

            throw new JspException("An error occurred building the tag.  Tag = " + this.getClass().getName()
                    + "\r\nRequest = " + this.getRequest().getRequestURL(), t);
        }

        JspWriter writer = pageContext.getOut();
        try {
            log.debug("doStartTag() - writing tag output");
            writer.print(html);
        } catch (IOException ex) {
            throw new SystemException("IOException attempting to write tag to output. " + ex.getMessage());
        }

        return (result);
    }

    public boolean hasPermission() {
        boolean access = true;
        if (permission != null) {
            access = false;
            for (SecurityGroup securityGroup : this.getAuthPerson().getUser().getSecurityGroups()) {
                for (Permission permission : securityGroup.getSecurityGroupRoles()) {
                    if (permission.getName().toString().equalsIgnoreCase(this.permission.toString())) {
                        return true;
                    }
                }
            }
        }
        return access;
    }
    
    public boolean hasPermission(String perm) {
    	boolean access = true;
        if (perm != null) {
            access = false;
            for (SecurityGroup securityGroup : this.getAuthPerson().getUser().getSecurityGroups()) {
                for (Permission permission : securityGroup.getSecurityGroupRoles()) {
                    if (permission.getName().toString().equalsIgnoreCase(perm.toString())) {
                        return true;
                    }
                }
            }
        }
        return access;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

}
